﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZhonTai.Admin.Core.Dto;
using ZhonTai.Admin.Services;
using ZhonTai.DynamicApi;
using ZhonTai.DynamicApi.Attributes;
using ZhonTai.Industry.Core.Consts;
using ZhonTai.Industry.Domain.ProductRoute;
using ZhonTai.Industry.Services.ProductRoute.Dto.Input;
using ZhonTai.Industry.Services.ProductRoute.Dto.Output;

namespace ZhonTai.Industry.Services.ProductRoute
{
    /// <summary>
    /// 产品路线服务
    /// </summary>
    [Order(20)]
    [DynamicApi(Area = IndustryConsts.AreaName)]
    public class ProductRouteService : BaseService, IProductRouteService, IDynamicApi
    {
        private readonly IProductRouteRepository _productRouteRepository;

        public ProductRouteService(IProductRouteRepository productRouteRepository)
        {
            _productRouteRepository = productRouteRepository;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ProductRouteGetOutput> GetAsync(long id)
        {
            var a = await _productRouteRepository.Select
                .WhereDynamic(id)
                .ToOneAsync<ProductRouteGetListOutput>();
            return a;
        }

        /// <summary>
        /// 列表查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IEnumerable<ProductRouteGetListOutput>> GetListAsync(ProductRouteGetListInput input)
        {
            var list = await _productRouteRepository.Select
                .OrderBy(a => a.Sort)
                .ToListAsync<ProductRouteGetListOutput>();
            return list;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageOutput<ProductRouteGetPageOutput>> GetPageAsync(PageInput<ProductRouteGetPageInput> input)
        {
            var filter = input.Filter;
            var select = _productRouteRepository.Select
                .WhereDynamicFilter(input.DynamicFilter)
                .Count(out var total);
            if (input.SortList != null && input.SortList.Count > 0)
            {
                input.SortList.ForEach(sort =>
                {
                    select = select.OrderByPropertyNameIf(sort.Order.HasValue, sort.PropName, sort.IsAscending.Value);
                });
            }
            else
            {
                select = select.OrderBy(a => a.Sort);
            }
            var list = await select
            .Page(input.CurrentPage, input.PageSize)
            .ToListAsync<ProductRouteGetPageOutput>();
            var data = new PageOutput<ProductRouteGetPageOutput>()
            {
                List = list,
                Total = total
            };
            return data;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<long> AddAsync(ProductRouteAddInput input)
        {
            var entity = Mapper.Map<ProductRouteEntity>(input);
            var id = (await _productRouteRepository.InsertAsync(entity)).Id;

            return id;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task UpdateAsync(ProductRouteUpdateInput input)
        {
            var entity = await _productRouteRepository.GetAsync(input.Id);
            if (!(entity?.Id > 0))
            {
                throw ResultOutput.Exception("产品路线详情不存在！");
            }

            Mapper.Map(input, entity);
            await _productRouteRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<bool> DeleteAsync(long id)
        {
            return await _productRouteRepository.DeleteAsync(id) > 0;
        }

        /// <summary>
        /// 软删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<bool> SoftDeleteAsync(long id)
        {
            return await _productRouteRepository.SoftDeleteAsync(id);
        }

        /// <summary>
        /// 批量软删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<bool> BatchSoftDeleteAsync(long[] ids)
        {
            return await _productRouteRepository.SoftDeleteAsync(ids);
        }
        
    }
}